package com.sibu.yx.promotion.persistence.support.typeHanders;

import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Arrays;
import java.util.List;

import org.apache.ibatis.type.BaseTypeHandler;
import org.apache.ibatis.type.JdbcType;

/**
 * @author yuwenbo
 * @create 2018-10-31 16:05
 **/
public class ListToStringTypeHandler extends BaseTypeHandler<List<String>> {

    @Override
    public void setNonNullParameter(PreparedStatement ps, int i, List<String> parameter, JdbcType jdbcType) throws SQLException {
        StringBuffer splitStr=new StringBuffer();
        for(int j=0;j<parameter.size();j++){
            if(j!=0){
                splitStr.append(",");
            }
            splitStr.append(parameter.get(j));
        }
        ps.setString(i,splitStr.toString());
    }

    @Override
    public List<String> getNullableResult(ResultSet rs, String columnName) throws SQLException {
        String strValue=rs.getString(columnName);
        List<String> result =Arrays.asList(strValue.split(","));
        return result;
    }

    @Override
    public List<String> getNullableResult(ResultSet rs, int columnIndex) throws SQLException {
        String strValue=rs.getString(columnIndex);
        List<String> result =Arrays.asList(strValue.split(","));
        return result;
    }

    @Override
    public List<String> getNullableResult(CallableStatement cs, int columnIndex) throws SQLException {
        String strValue=cs.getString(columnIndex);
        List<String> result =Arrays.asList(strValue.split(","));
        return result;
    }
}
